package com.meilai.project.controller.web.personnel.onTurn;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.personnel.onTurn.LeaveOfficeApplyCheckDTO;
import com.meilai.project.dto.personnel.onTurn.LeaveOfficeApplyDTO;
import com.meilai.project.service.personnel.onTurn.LeaveOfficeApplyService;
import com.meilai.project.vo.personnel.onTurn.LeaveOfficeApplyVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "离职申请", tags = {"离职申请"})
@RestController
@RequestMapping("api/web/leave_office_apply")
public class LeaveOfficeApplyController {

    @Autowired
    private LeaveOfficeApplyService service;

    @ApiOperation(value = "列表分页查询", notes = "人事，查看所有考察登记记录", response = LeaveOfficeApplyVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("list")
    public CommonResult list(
            @ApiParam(name = "page", value = "当前页", required = true)
            @RequestParam(value = "page", defaultValue = "1", required = true) Integer page,
            @ApiParam(name = "page_size", value = "每页数量", required = true)
            @RequestParam(value = "page_size", defaultValue = "10", required = true) Integer page_size,
            @ApiParam(name = "flow_status", value = "当前状态", required = false)
            @RequestParam(value = "flow_status", required = false) Integer flow_status,
            @ApiParam(name = "name", value = "姓名", required = false)
            @RequestParam(value = "name", required = false) String name,
            @ApiParam(name = "plan_leave_date_min", value = "拟离职日期最小值，时间段搜索的起始，yyyy-MM-dd", required = false)
            @RequestParam(value = "plan_leave_date_min", required = false) String plan_leave_date_min,
            @ApiParam(name = "plan_leave_date_max", value = "拟离职日期最大值，时间段搜索的结束，yyyy-MM-dd", required = false)
            @RequestParam(value = "plan_leave_date_max", required = false) String plan_leave_date_max,
            @ApiParam(name = "contract_end_at_min", value = "合同截止日期最小值，时间段搜索的起始，yyyy-MM-dd", required = false)
            @RequestParam(value = "contract_end_at_min", required = false) String contract_end_at_min,
            @ApiParam(name = "contract_end_at_max", value = "合同截止日期最大值，时间段搜索的起始，yyyy-MM-dd", required = false)
            @RequestParam(value = "contract_end_at_max", required = false) String contract_end_at_max,
            @ApiParam(name = "staff_type_id", value = "员工类型id", required = false)
            @RequestParam(value = "staff_type_id", required = false) Integer staff_type_id) {

        LeaveOfficeApplyDTO searchDTO = new LeaveOfficeApplyDTO();
        searchDTO.setFlow_status(flow_status);
        searchDTO.setName(name);
        searchDTO.setPlan_leave_date_min(plan_leave_date_min);
        searchDTO.setPlan_leave_date_max(plan_leave_date_max);
        searchDTO.setContract_end_at_min(contract_end_at_min);
        searchDTO.setContract_end_at_max(contract_end_at_max);
        searchDTO.setStaff_type_id(staff_type_id);

        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }


    @ApiOperation(value = "本人列表查询", notes = "自己的离职申请单列表记录", response = LeaveOfficeApplyVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("own_list")
    public CommonResult Ownlist() {

        return new CommonResult().success(service.selectOwnList());
    }

    @ApiOperation(value = "详情", notes = "详情", response = LeaveOfficeApplyVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("detail/{id}")
    public CommonResult detail(@ApiParam(name = "id", value = "数据id", required = true) @PathVariable("id") Long id) {
        return new CommonResult().success(service.getOneById(id));
    }

    @ApiOperation(value = "新离职申请回填内容", notes = "新离职申请回填内容", response = LeaveOfficeApplyVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("new_appley_detail")
    public CommonResult newAppleyDetail() {
        return new CommonResult().success(service.getNewAppleyDetail());
    }

    @ApiOperation(value = "保存", notes = "保存")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("save")
    public CommonResult save(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) LeaveOfficeApplyDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.save(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "提交", notes = "提交")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("begin")
    public CommonResult begin(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) LeaveOfficeApplyDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.begin(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "通过", notes = "通过")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("pass")
    public CommonResult pass(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) LeaveOfficeApplyCheckDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.pass(dto)) {
                return new CommonResult().success("通过成功");
            } else {
                return new CommonResult().failed("通过失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "驳回", notes = "驳回")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("return_back")
    public CommonResult returnBack(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) LeaveOfficeApplyCheckDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.returnBack(dto)) {
                return new CommonResult().success("驳回成功");
            } else {
                return new CommonResult().failed("驳回失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("delete")
    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.delete(id)) {
                return new CommonResult().success("删除成功");
            } else {
                return new CommonResult().failed("删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "彻底删除", notes = "彻底删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("actual_delete")
    public CommonResult actualDelete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.actualDelete(id)) {
                return new CommonResult().success("彻底删除成功");
            } else {
                return new CommonResult().failed("彻底删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "上传离职交接单", notes = "上传离职交接单")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("upload_handover_sheet")
    public CommonResult uploadHandoverSheet(
            @ApiParam(name = "handover_sheet_url", value = "链接字符串", required = true)
            @RequestParam(value = "handover_sheet_url", required = true) String handover_sheet_url,
            @ApiParam(name = "leave_office_apply_id", value = "离职申请单ID", required = true)
            @RequestParam(value = "leave_office_apply_id", required = true) Long leave_office_apply_id
    ) {
        try {
            if (service.uploadHandoverSheet(handover_sheet_url,leave_office_apply_id)) {
                return new CommonResult().success("上传离职交接单成功");
            } else {
                return new CommonResult().failed("上传离职交接单失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }
}
